//
//  NewfeatureViewController.swift
//  ZHTQWeibo
//
//  Created by ZHTQ on 2018/9/19.
//  Copyright © 2018年 ZHTQ. All rights reserved.
//

import UIKit

class NewfeatureViewController: UIViewController {

    private let maxCount = 4
    override func viewDidLoad() {
        super.viewDidLoad()

    }

}
//MARK:- NewfeatureViewCell
class NewfeatureViewCell: UICollectionViewCell {
    /// 记录当前的页数
    var index: Int = 1 {
        didSet {
            
            iconView.image = UIImage(named: "new_feature_\(index)")
            lastButton.isHidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        // 初始化
        setup()
    }
    
    //MARK:- 外部控制方法
    /// 按钮的缩放动画
    func lastButtonAnimation() {
        lastButton.isHidden = false
        // 做动画
        lastButton.transform = CGAffineTransform(scaleX: 0, y: 0)
        
        UIView.animate(withDuration: 1.0, delay: 0.25, usingSpringWithDamping: 0.4, initialSpringVelocity: 2, options: UIViewAnimationOptions(rawValue: 0), animations: {
            
            self.lastButton.transform = CGAffineTransform.identity
            self.lastButton.isUserInteractionEnabled = false
        }) { (_) in
            self.lastButton.isUserInteractionEnabled = true
        }
    }
    
    //MARK:- 内部控制方法
    private func setup(){
        
        // 添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(lastButton)
        //布局子控件
        
        iconView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        
        lastButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-50)
        }
        
    }
    
    ///按钮点击
    @objc private func lastButtonClick(){
        // 点击按钮发送跳转主界面的通知
        NotificationCenter.default.post(name: NSNotification.Name(ZHTQViewControllerChangeNotification), object: true)
    }
    //MARK:- 懒加载
    /// 界面图片
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView(image: #imageLiteral(resourceName: "new_feature_1"))
        return imageView
    }()
    
    /// 最后一页的按钮
    private lazy var lastButton: UIButton = {
       let btn = UIButton(image: "new_feature_button", backgroundImage: "new_feature_finish_button")
        btn.addTarget(self, action: #selector(lastButtonClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
}

//MARK:-NewfeatureViewFlowLayout
class NewfeatureViewFlowLayout: UICollectionViewFlowLayout{
    override func prepare() {
        // cell的尺寸
        itemSize = UIScreen.main.bounds.size
        // cell的间距
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        // 滚动方向
        scrollDirection = UICollectionViewScrollDirection.horizontal
        // 滚动条取消
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        // 取消弹簧效果
        collectionView?.bounces = false
        // 开启分页
        collectionView?.isPagingEnabled = true
    }
}

// MARK:- UICollectionViewDataSource
extension NewfeatureViewController: UICollectionViewDataSource{
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "newfeatureCell", for: indexPath) as! NewfeatureViewCell
        
        cell.index = indexPath.row + 1
        
        return cell
    }
}
// MARK:- UICollectionViewDelegate
extension NewfeatureViewController: UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        let cell = collectionView.visibleCells.last as! NewfeatureViewCell
        
        if cell.index == maxCount {
            cell.lastButtonAnimation()
        }
     }
    
}
